local skill = fk.CreateSkill{
  name = "tea__huize"
}
local tableSwap = function(Table, Pos1, Pos2)
  Table[Pos1], Table[Pos2] = Table[Pos2], Table[Pos1]
  return Table
end
local setZePile = function(player, cids, skillName)
  player:addToPile("$tea__ze_pile", cids, true, skillName, player.id)
end
---@type TrigSkelSpec<TrigFunc>
local huize = {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.GamePrepared then
      return true
    else
      return player == target and player.phase == Player.Start
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.GamePrepared then
      return true
    else
      return player.room:askToSkillInvoke(player, {skill_name = skill.name})
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GamePrepared then
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(skill.name, 1)
      local cids = room:getNCards(5)
      setZePile(player, cids, self.name)
    else
      room:notifySkillInvoked(player, self.name, "special")
      player:broadcastSkillInvoke(skill.name, table.random{2, 3})
      if #(player:getPile("$tea__ze_pile") or {}) < 5 then return end
      local cards_data = {{"$tea__ze_pile", player:getPile("$tea__ze_pile")}}
      local choose_data = {
        to = nil,
        min = 2,
        max = 2,
        skillName = self.name,
        prompt = "#tea__kangze-choose",
      }
      local ret = room:askToPoxi(player, {
        poxi_type = "AskForCardsChosen",
        data = cards_data,
        extra_data = choose_data,
        cancelable = false,
      })
      local new_ret = table.filter(ret, function(id) return id ~= -1 end)
      local hidden_num = #ret - #new_ret
      if hidden_num > 0 then
        table.insertTable(new_ret,
        table.random(player:getPile("$tea__ze_pile"), hidden_num))
      end
      if #new_ret == 2 then
        local zePile = player:getPile("$tea__ze_pile")
        if table.contains(zePile, new_ret[1]) and table.contains(zePile, new_ret[2]) then
          player:showCards(new_ret)
          local swapPile = tableSwap(zePile, table.indexOf(zePile, new_ret[1]), table.indexOf(zePile, new_ret[2]))
          room:sendLog{
            type = "#KangZeSwapCards",
            from = player.id,
          }
          player:addToPile("$tea__ze_pile", swapPile, true, self.name, player.id)
          local num = math.max(math.abs(table.indexOf(zePile, new_ret[1]) - table.indexOf(zePile, new_ret[2])) - 1, 0)
          if num > 0 then
            local targets = room:askToChoosePlayers(player, {
              targets = room.alive_players,
              skill_name = skill.name,
              min_num = 1,
              max_num = num,
              prompt = "#tea__kangze-drawcards:::"..num,
            })
            if #targets > 0 then
              room:sortByAction(targets)
              for _, p in ipairs(targets) do
                if not p.dead then p:drawCards(1, self.name) end
              end
            end
          end
        end
      end
    end
  end,
}
skill:addEffect(fk.GamePrepared, huize)
skill:addEffect(fk.EventPhaseStart, huize)
return skill